package mk.com.ldm.corepoint.implementation.client.view;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ModalOverlay extends Composite{

//	private static Timer timer;
	private static VerticalPanel panel = new VerticalPanel();
	private static ModalOverlay modalOverlay = null;
	private static HTMLPanel imagePanel = new HTMLPanel("<img src='images/modal-loader.gif' />");
	private static Boolean visible = false;
	
	public static ModalOverlay getInstance(){
		
		if(modalOverlay==null){
			modalOverlay = new ModalOverlay();
		}
			
		return modalOverlay;
	}
	
	private static Timer timer = new Timer() {
		public void run() {
			imagePanel.setStyleName("lt-Modal-loader");
			panel.add(imagePanel);
			panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
			panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			Element elem = panel.getElement();
			elem.addClassName("lt-Modal");
			resize();
			RootPanel.get().add(panel);
			visible = true;
		}
	};
	
	public static void show(){
		timer.schedule(1);
		
	}
	
	public static void hide(){
		timer.cancel();
		if(visible){
			RootPanel.get().remove(panel);
			panel.remove(imagePanel);
			visible = false;
		}
	}
	
	
	public static void resize(){
		panel.setSize(Window.getClientWidth() + "px",
                Window.getClientHeight() + "px");
	}
	
	
	
}
